package sg.edu.nus.iss.ooad.jass.dao.file;

import java.util.ArrayList;
import java.util.List;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.IResponseDAO;
import sg.edu.nus.iss.ooad.jass.entity.IJASSEntity;
import sg.edu.nus.iss.ooad.jass.entity.JobAdvertisement;
import sg.edu.nus.iss.ooad.jass.entity.Jobseeker;
import sg.edu.nus.iss.ooad.jass.entity.Response;

/**
 *
 * Response Data Access Object for Properties File implementation.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : ResponseDAOPropertiesImpl.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class ResponseDAOPropertiesImpl extends AbstractPropertiesDAO implements IResponseDAO {

        //Responses List
        public static Hashtable<String, IJASSEntity> responseCache = new Hashtable<String, IJASSEntity>();
        //Logger
        private final static Logger logger = Logger.getLogger(ResponseDAOPropertiesImpl.class.getName());
        //Properties filename
        public static final String RESPONSE_FILE = "data.response.properties";
        //Variable to hold Properties for Response
        public static Properties properties = null;

        //Load at start-up
        static {
                logger.info("Start loading " + RESPONSE_FILE);
                properties = loadProperties(RESPONSE_FILE);
                if (properties != null) {
                        Response response = new Response();
                        responseCache = response.fromJASSPropertiesFormat(properties);
                }
                logger.info("Finish loading " + RESPONSE_FILE);
        }

        /**
         * Constructor
         */
        public ResponseDAOPropertiesImpl() {
        }

        /**
         * Create new Response entity and store it to the persistent data.
         * @param response
         * @return
         */
        public synchronized boolean createResponse(Response response) {
                boolean retValue = false;

                //get number to total record
                int iTotalRecord = 0;
                String sTotalRecord = properties.getProperty("NumOfItems");
                if (sTotalRecord != null) {
                        iTotalRecord = Integer.parseInt(sTotalRecord);
                }
                iTotalRecord = iTotalRecord + 1;
                response.setID(String.valueOf(iTotalRecord));

                //update properties
                //RESPONSE_ATTRIBUTES = {"ResponseID", "JobAdvertisementID", "JobSeekerID", "ResponseDate", "ResponseStatus"};
                properties.setProperty("NumOfItems", String.valueOf(iTotalRecord));
                properties.put("ResponseID" + String.valueOf(iTotalRecord), response.getID());
                properties.put("JobAdvertisementID" + String.valueOf(iTotalRecord), response.getJobAdvertisementID());
                properties.put("JobSeekerID" + String.valueOf(iTotalRecord), response.getJobseekerID());
                properties.put("ResponseDate" + String.valueOf(iTotalRecord), response.getResponseDate());
                properties.put("ResponseStatus" + String.valueOf(iTotalRecord), response.getResponseStatus());

                //save it and update the cache
                retValue = storeProperties(properties, RESPONSE_FILE);
                responseCache.put(response.getID(), response);

                //cascase update
                //response -- job advertisement
                JobAdvertisement jobadvertisement = (JobAdvertisement) JobAdvertisementDAOPropertiesImpl.jobadvertisementCache.get(response.getJobAdvertisementID());
                jobadvertisement.getResponse().add(response);
                //response -- jobseeker
                Jobseeker jobseeker = (Jobseeker) JobseekerDAOPropertiesImpl.jobseekerCache.get(response.getJobseekerID());
                jobseeker.getResponse().add(response);


                return retValue;
        }

        /**
         * Retrieve an Response from persistent data.
         * @param ID
         * @return
         */
        public Response retrieveResponse(String ID) {
                Response retValue = null;

                retValue = (Response) responseCache.get(ID);

                return retValue;
        }

        public synchronized boolean updateResponse(Response response) {
                boolean retValue = false;

                if (responseCache.containsKey(response.getID())) {

                        //update properties
                        //RESPONSE_ATTRIBUTES = {"ResponseID", "JobAdvertisementID", "JobSeekerID", "ResponseDate", "ResponseStatus"};
                        properties.setProperty("JobAdvertisementID" + response.getID(), response.getJobAdvertisementID());
                        properties.setProperty("JobSeekerID" + response.getID(), response.getJobseekerID());
                        properties.setProperty("ResponseDate" + response.getID(), response.getResponseDate());
                        properties.setProperty("ResponseStatus" + response.getID(), response.getResponseStatus());

                        //save it and update the List
                        storeProperties(properties, RESPONSE_FILE);
                        responseCache.remove(response.getID());
                        responseCache.put(response.getID(), response);

                        //cascase update
                        //response -- job advertisement
                        JobAdvertisement jobadvertisement = (JobAdvertisement) JobAdvertisementDAOPropertiesImpl.jobadvertisementCache.get(response.getJobAdvertisementID());
                        for (Iterator<Response> i = jobadvertisement.getResponse().iterator(); i.hasNext();) {
                                Response dataObject = i.next();
                                if (dataObject.getID().toLowerCase().equalsIgnoreCase(response.getID())) {
                                        i.remove();
                                } else {
                                        continue;
                                }
                        }
                        jobadvertisement.getResponse().add(response);
                        //response -- jobseeker
                        Jobseeker jobseeker = (Jobseeker) JobseekerDAOPropertiesImpl.jobseekerCache.get(response.getJobseekerID());
                        for (Iterator<Response> i = (Iterator<Response>) jobseeker.getResponse().iterator(); i.hasNext();) {
                                Response dataObject = i.next();
                                if (dataObject.getID().toLowerCase().equalsIgnoreCase(response.getID())) {
                                        i.remove();
                                } else {
                                        continue;
                                }
                        }
                        jobseeker.getResponse().add(response);

                        retValue = true;
                }

                return retValue;
        }

        /**
         *
         * @param response
         * @return
         */
        public boolean deleteResponse(Response response) {
                throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         *
         * @return
         */
        public List<Response> getAllResponse() {
                List<Response> retValue = new ArrayList<Response>();

                //Looping the whole records from cache
                Enumeration<IJASSEntity> elements = responseCache.elements();
                while (elements.hasMoreElements()) {
                        Response response = (Response) elements.nextElement();
                        retValue.add(response);
                }

                return retValue;
        }

        /**
         *
         * @param jobadvertisementID
         * @return
         */
        public List<Response> findResponseByJobAdvertisementID(String jobadvertisementID) {
                List<Response> retValue = new ArrayList<Response>();

                Enumeration<IJASSEntity> elements = responseCache.elements();
                while (elements.hasMoreElements()) {
                        Response response = (Response) elements.nextElement();
                        if (response.getJobAdvertisementID().equalsIgnoreCase(jobadvertisementID)) {
                                retValue.add(response);
                        }
                }

                return retValue;
        }

        /**
         * 
         * @param jobseekerID
         * @return
         */
        public List<Response> findResponseByJobseekerID(String jobseekerID) {
                List<Response> retValue = new ArrayList<Response>();

                Enumeration<IJASSEntity> elements = responseCache.elements();
                while (elements.hasMoreElements()) {
                        Response response = (Response) elements.nextElement();
                        if (response.getJobAdvertisementID().equalsIgnoreCase(jobseekerID)) {
                                retValue.add(response);
                        }
                }

                return retValue;
        }

        /**
         *
         * @return
         */
        public static synchronized Hashtable<String, IJASSEntity> cache() {
                return responseCache;
        }

}
